using System.Collections.Generic;

namespace RFS.Domain.PaiShi.Accounting
{
    
    public static class AmountExtensions
    {
        public static Amount Sum(this IEnumerable<Amount> amounts)
        {
            Amount sum = new Amount();
            foreach (var amount in amounts)
            {
                sum += amount; // Uses the overloaded + operator
            }
            return sum;
        }
    }
    
    
    public struct Amount
    {
        public override string ToString()
        {
            // var qs=Quantity==0?"-":Quantity.ToString();
            // var cs=AmountCash==0?"-":Quantity.ToString("#,###.##");
            if (Quantity == 0 && AmountCash == 0) return "-";
            
            return  $"{Quantity:#,###.##;-#,###.##;-}/{AmountCash:#,###.##;-#,###.##;-}";
        }

        public Amount(decimal amount=0, decimal quantity=0)
        {
            AmountCash = amount;
            Quantity = quantity;
        }

        public decimal Quantity { get; set; }
        public decimal AmountCash { get; set; }
        
        // Override the + operator
        public static Amount operator +(Amount a, Amount b)
        {
            return new Amount
            {
                AmountCash = a.AmountCash + b.AmountCash,
                Quantity = a.Quantity + b.Quantity
            };
        }

        // Override the - operator
        public static Amount operator -(Amount a, Amount b)
        {
            return new Amount
            {
                AmountCash = a.AmountCash - b.AmountCash,
                Quantity = a.Quantity - b.Quantity
            };
        }
        
        // Implement IEquatable<Amount>
        public bool Equals(Amount other)
        {
            return AmountCash == other.AmountCash && Quantity == other.Quantity;
        }

        // Override object.Equals
        public override bool Equals(object obj)
        {
            return obj is Amount other && Equals(other);
        }

        // Override object.GetHashCode
        public override int GetHashCode()
        {
            unchecked
            {
                return (AmountCash.GetHashCode() * 397) ^ Quantity.GetHashCode();
            }

        }
        
        
    }
}